package com.sgam.mf.domain.stock;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.format.annotation.DateTimeFormat;

import com.sgam.mf.domain.entity.AbstractAuditableEntity;
import com.sgam.mf.domain.entity.Contracts;
import com.sgam.mf.domain.sec.SecUser;
import com.sgam.utils.CustomDateDeserializer;
import com.sgam.utils.CustomDateSerializer;

@Entity
@Table(name = "STOCK_DOCUMENT")
public class StockDocument extends AbstractAuditableEntity implements Comparable<StockDocument>  {

	//descending
	public int compareTo(StockDocument o) {
		  if (o.getDocumentData().after(this.documentData)) return 1;
	      if (o.getDocumentData().equals(this.documentData)) return 0;
	      return -1;
	}
	
	@ManyToOne
	@JoinColumn(name = "document_type_fk")		
    private StockDocumentType stockDocumentType;
	
     
    @JsonSerialize(using = CustomDateSerializer.class)
	@JsonDeserialize(using = CustomDateDeserializer.class)
	@DateTimeFormat(pattern="dd.MM.yyyy")
	@Column(name = "DOCUMENT_DATA")
    private Date documentData;
    
    @Column(name = "DOCUMENT_COMMENT")
    private String documentComment;
    
    @ManyToOne
	@JoinColumn(name = "CONTRACTS_FK")		
    private Contracts contracts;

    @ManyToOne
	@JoinColumn(name = "dt_sec_user_fk")		
    private SecUser dtUser;
	 
	@ManyToOne
	@JoinColumn(name = "ct_sec_user_fk")		
    private SecUser ctUser;	
	
	@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER, orphanRemoval=true)
	@Fetch(value = FetchMode.SUBSELECT)
	@JoinColumn(name="STOCK_DOCUMENT_FK")
	private List<StockOperation> stockOperation = new ArrayList<StockOperation>();


	public StockDocumentType getStockDocumentType() {
		return stockDocumentType;
	}

	public void setStockDocumentType(StockDocumentType stockDocumentType) {
		this.stockDocumentType = stockDocumentType;
	}

	public Date getDocumentData() {
		return documentData;
	}

	public void setDocumentData(Date documentData) {
		this.documentData = documentData;
	}

	public String getDocumentComment() {
		return documentComment;
	}

	public void setDocumentComment(String documentComment) {
		this.documentComment = documentComment;
	}

	public Contracts getContracts() {
		return contracts;
	}

	public void setContracts(Contracts contracts) {
		this.contracts = contracts;
	}

	public SecUser getDtUser() {
		return dtUser;
	}

	public void setDtUser(SecUser dtUser) {
		this.dtUser = dtUser;
	}

	public SecUser getCtUser() {
		return ctUser;
	}

	public void setCtUser(SecUser ctUser) {
		this.ctUser = ctUser;
	}

	public List<StockOperation> getStockOperation() {
		return stockOperation;
	}

	public void setStockOperation(List<StockOperation> stockOperation) {
		this.stockOperation = stockOperation;
	}


	
}
